#include <jni.h>
#include <string>


extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_skin_1whitening_1ndk_jni_stringFromJNI(JNIEnv *env, jobject instance) {

    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jintArray JNICALL
Java_com_example_skin_1whitening_1ndk_jni_getNdkBitmap(JNIEnv *env, jobject instance, jintArray buffer,
                                                       jint width, jint height) {
jint *data=env->GetIntArrayElements(buffer,NULL);
    if (data == NULL) {
        return 0; /* exception occurred */
    }
    int alpha = 0xFF << 24;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            // 获得像素的颜色
            int color = data[width * i + j];
            int red = ((color & 0x00FF0000) >> 16);
            int green = ((color & 0x0000FF00) >> 8);
            int blue = color & 0x000000FF;
            color = (red + green + blue) / 3;
            color = alpha | (color << 16) | (color << 8) | color;
            data[width * i + j] = color;
        }
    }
    int size=width * height;
    jintArray result = env->NewIntArray(size);
    env->SetIntArrayRegion(result, 0, size, data);
    env->ReleaseIntArrayElements(buffer, data, 0);
    return result;
}
//色度增量
     float brightness=0.2f;
//对比度
     float contrainst=0.f;
extern "C"
JNIEXPORT jintArray JNICALL
Java_com_example_skin_1whitening_1ndk_jni_getSkinBitmap(JNIEnv *env, jobject instance,
                                                        jintArray buffer_, jint width,
                                                        jint height) {
    jint *data = env->GetIntArrayElements(buffer_, NULL);
        if (data == NULL) {
        return 0; /* exception occurred */
    }
    int a,r,g,b;
    int bab= (int) (255*brightness);
    float ca=1.0f+contrainst;
    int color;
    //依次拿到每一个像素点
    for (int x=0;x<width;x++){
        for (int y=0;y<height;y++){
            //width*y+x是一维数组下标（注意这里Bitmap是二位数组，
            // jintArray是一维度数组）用一维数组表示二维像素点
            color= data[width*y+x];
            a=color >> 24;//得到透明度
            r=(color >> 16) & 0xFF;//得到红色
            g=(color >> 8) & 0xFF;//得到绿设
            b= color & 0xFF;//得到蓝色
            //增加色度
            int ri=r+bab;
            int gi=g+bab;
            int bi=b+bab;
            //防溢出检测（0~255）
            r=ri>255?255:(ri<0?0:ri);
            g=gi>255?255:(gi<0?0:gi);
            b=bi>255?255:(bi<0?0:bi);
            //扩大对比度,以128为界限，拉大两边的差距
            ri=r-128;
            gi=g-128;
            bi=b-128;

            ri= (int) (ri*ca);
            gi= (int) (gi*ca);
            bi= (int) (bi*ca);

            ri=ri+128;
            gi=gi+128;
            bi=bi+128;

            //防溢出检测（0~255）
            r=ri>255?255:(ri<0?0:ri);
            g=gi>255?255:(gi<0?0:gi);
            b=bi>255?255:(bi<0?0:bi);
            //合成像素
            data[width*y+x]=(a << 24) | (r << 16) | (g << 8) | b;
        }
    }
    int size=width * height;
    //创建一个result大小是size
    jintArray result = env->NewIntArray(size);
    //数据保存到result中
    env->SetIntArrayRegion(result, 0, size, data);
    //释放资源（传进来的buffer_，和data指针）
    env->ReleaseIntArrayElements(buffer_, data, 0);
    return result;
}